#include<string.h>
#include <iostream>
using std::cout;
using std::endl;
class Computer{
public:

    Computer(int price,const char *brand)
    :_price(price)
    ,_brand(new char[strlen(brand)+1]())
    {
        strcpy(_brand,brand);
    }
    //公有析构函数（栈对象必须公有)
    ~Computer(){
        if(_brand != NULL){
            delete[] _brand;
        }
    }
    //拷贝构造函数
    Computer(const Computer&rhs)
    :_price(rhs._price)
    ,_brand(new char[strlen(rhs._brand)+1])
    {
        strcpy(_brand,rhs._brand);
    }
    //拷贝赋值函数
    Computer & operator=(const Computer&rhs)
    {
        if(this == &rhs){
            return *this;
        }
        delete [] _brand;
        _price = rhs._price;
        _brand = new char[strlen(rhs._brand)+1];
        strcpy(_brand,rhs._brand);
        return *this;
    }
    void print() const {
        std::cout << "Price: " << _price << ", Brand: " << _brand << std::endl;
    }
private:
    //将new /delete设为私有
    void * operator new (size_t size) = delete;
    void * operator new [](size_t size) = delete;
    void operator delete (void *ptr) = delete;
    void operator delete [](void * ptr) = delete;
    int _price;
    char * _brand;
};
void test(){
   Computer pc1(5000,"HUAWEI");
   Computer pc2 = pc1;
   Computer pc3(6000,"APPLE");
   pc1.print();
   pc3 = pc1;
   pc2.print();
   pc3.print();
}
int main()
{
    test();
    return 0;
}

